package tcu.iot.gasmanage.mapper;

import org.apache.ibatis.annotations.Mapper;
import tcu.iot.gasmanage.common.utils.GetActionDataUtils;
import tcu.iot.gasmanage.common.utils.JSONUtils;
import tcu.iot.gasmanage.entity.datamanagement.UpdateRealTimeData;
import tcu.iot.gasmanage.entity.equipment.EquipmentData;
import tcu.iot.gasmanage.entity.equipment.EquipmentInfoResult;
import tcu.iot.gasmanage.entity.export.TDActionData;

import java.util.ArrayList;
import java.util.List;

@Mapper
public class TDengineMapper {
    //DataTableService
    public static List<EquipmentInfoResult> DateEquipmentInfoData(String equipmentNumber, String startDate, String endDate) {
        return JSONUtils.parseEquipmentData("SELECT * FROM electory." + equipmentNumber + " WHERE ts >= '" + startDate
                + "' AND ts <= '" + endDate + "' order by ts desc;");
    }

    public static int DateEquipmentInfoTotal(String equipmentNumber, String startDate, String endDate) {
        return JSONUtils.getTotal("SELECT count(*) FROM electory." + equipmentNumber + " WHERE ts >= '" + startDate
                + "' AND ts <= '" + endDate + "';");
    }

    public static List<EquipmentInfoResult> AllEquipmentInfoData(String equipmentNumber) {
        return JSONUtils.parseEquipmentData("SELECT * FROM electory." + equipmentNumber + " order by ts desc;");
    }

    public static int AllEquipmentInfoTotal(String equipmentNumber) {
        return JSONUtils.getTotal("SELECT count(*) FROM electory." + equipmentNumber + ";");
    }

    //LineChartService
    public static List<EquipmentInfoResult> DateLineChartData(String equipmentNumber, String startDate, String endDate) {
        return JSONUtils.parseEquipmentData("SELECT * FROM electory." + equipmentNumber + " WHERE ts >= '" + startDate
                + "' AND ts <= '" + endDate + "' order by ts desc;");
    }

    public static List<EquipmentInfoResult> AllLineChartData(String equipmentNumber) {
        return JSONUtils.parseEquipmentData("SELECT * FROM electory." + equipmentNumber + " order by ts desc;");
    }

    //HomeService
    public static List<EquipmentData> HomeBarChartData(List<String> list, List<String> signalList) {
        List<EquipmentData> equipmentDataList = new ArrayList<>();

        return GetActionDataUtils.AllActionList(list, signalList);
    }


    public static List<EquipmentInfoResult> UpdateData(String equipmentNumber) {
        return JSONUtils.parseEquipmentData("SELECT * FROM electory." + equipmentNumber + " order by ts desc LIMIT 30;");
    }

    //EquipmentService
    public static List<String> GetAllTables() {
        return JSONUtils.getAllTables("SHOW electory.TABLES");
    }

    //ExportService
    public static List<TDActionData> ExportActionData(List<String> list) {
        List<TDActionData> tdActionData = new ArrayList<>();
        for (String s : list) {
            tdActionData.add(JSONUtils.getExportData("SELECT * FROM electory." + s + " order by ts desc LIMIT 1", s));
        }
        return tdActionData;
    }

    //DataManagementService
    public static List<UpdateRealTimeData> UpdateRealTimeData(String equipmentNumber, String dataType, String startDate, String endDate) {
        return JSONUtils.UpdateRealTimeData("SELECT ts," + dataType + " FROM electory." + equipmentNumber + " WHERE ts >= '" + startDate
                + "' AND ts <= '" + endDate + "' order by ts desc;");
    }

    public static List<UpdateRealTimeData> UpdateRealTimeDataWithoutDate(String equipmentNumber, String dataType) {
        return JSONUtils.UpdateRealTimeData("SELECT ts," + dataType + " FROM electory." + equipmentNumber + " order by ts desc LIMIT 30;");
    }
}
